package com.qf.controller;

import com.qf.entity.EndUser;
import com.qf.service.LoginService;
import org.jctools.maps.NonBlockingHashMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@RequestMapping("/login")
@RestController
@CrossOrigin("*")
public class LoginController {
    private LoginService loginService;

    public LoginController(LoginService loginService) {
        this.loginService = loginService;
    }


    @PostMapping
    public Object loginto(HttpSession session,String verificationCode,String endName,String endPassword){
        NonBlockingHashMap<String,Object> map = new NonBlockingHashMap<>();
        System.out.println(verificationCode+"---"+endName+"---"+endPassword);

      Object obj=  session.getAttribute("code");
      if (obj==null){
          map.put("code",-1);
          map.put("msg","验证码不能为空");
          return  map;
      }
       String code = (String) obj;
        System.out.println(code);
        if (!code.equalsIgnoreCase(verificationCode)){
            map.put("code",-1);
            map.put("msg","验证码错误");
            return  map;
        }
        EndUser endUser = loginService.queryByName(endName);
        if (endUser==null){
            map.put("code",0);
            map.put("msg","没有这个用户或用户被禁用");
           return  map;
        }
        EndUser endUser1 = loginService.queryByNameAndPassWord(endName, endPassword);
        if (endUser1 == null){
            map.put("code",1);
            map.put("msg","用户密码错误");
            return  map;
        }
        session.setAttribute("endUser",endUser1);
        map.put("code",2);
        map.put("data","index.html");
        return map;
    }


    @GetMapping("/out")
    public Object loginOut(HttpSession session){
        session.removeAttribute("endUser");
        NonBlockingHashMap<String,Integer> map =new NonBlockingHashMap<>();
        map.put("code",1);
        return map;
    }


}
